package com.huohuzhihui.web.controller.api;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.huohuzhihui.common.config.WxMaConfiguration;
import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.utils.StringUtils;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.service.IOrderService;
import com.huohuzhihui.merchant.service.IPayService;
import com.ijpay.core.kit.IpKit;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.wxpay.WxPayApi;
import com.ijpay.wxpay.WxPayApiConfig;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;

/**
 * 手机支付
 *
 * @author luzy
 * @date 2021-06-09
 */
@RestController
@RequestMapping("/api/pay")
public class ApiPayController extends BaseController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private IPayService payService;

    @Value("${wxpay.appId}")
    private String appId;

    @Value("${wxpay.appSecret}")
    private String appSecret;

    @Value("${wxpay.certPath}")
    private String certPath;

    @Value("${wxpay.domain}")
    private String domain;

    @Value("${wxpay.mchId}")
    private String mchId;

    @Value("${wxpay.partnerKey}")
    private String partnerKey;
    @Value("${wxpay.notifyUrl}")
    private String notifyUrl;
    @Value("${wxpay.refundNotifyUrl}")
    private String refundNotifyUrl;

    private WxPayApiConfig wxPayApiConfig;

    @PostConstruct
    public WxPayApiConfig getApiConfig() {
        wxPayApiConfig = WxPayApiConfig.builder()
                .appId(appId)
                .mchId(mchId)
                .partnerKey(partnerKey)
                .certPath(certPath)
                .domain(domain)
                .build();
        return wxPayApiConfig;
    }


    @RequestMapping("/getOpenid")
    public AjaxResult getUserInfo(String appid, @RequestParam(name = "code") String code) throws Exception {
        if (org.apache.commons.lang3.StringUtils.isBlank(code)) {
            AjaxResult ajax = AjaxResult.error("code不能为空！");
            return ajax;
        }
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
            return new AjaxResult(200, "获取微信用户openid成功", session.getOpenid());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return AjaxResult.error("获取微信用户openid失败");
    }

    @PostMapping("/prepare")
    public AjaxResult add(HttpServletRequest request, Long orderId, String openId) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getUser();
        String ip = IpKit.getRealIp(request);
        if (StringUtils.isBlank(ip)) {
            ip = "127.0.0.1";
        }
        return new AjaxResult(200, "获取支付参数成功", payService.prepare(orderId, openId, ip));
    }

    /**
     * 异步通知
     */
    @RequestMapping(value = "/wxPayNotify", method = {RequestMethod.POST, RequestMethod.GET})
    public String wxPayNotify(HttpServletRequest request) {
        return payService.notify(request);
    }


}
